package com.yfq.springframework.context.support;

import com.yfq.springframework.beans.BeansException;
import com.yfq.springframework.beans.factory.ConfigurableListableBeanFactory;
import com.yfq.springframework.beans.config.BeanFactoryPostProcessor;
import com.yfq.springframework.beans.config.BeanPostProcessor;
import com.yfq.springframework.context.ConfigurableApplicationContext;
import com.yfq.springframework.core.io.DefaultResourceLoader;

import java.util.Map;

/**
 * @author 姚凤桥
 * @date 2023/1/19 14:59
 * @description
 */
public abstract class AbstractApplicationContext extends DefaultResourceLoader
        implements ConfigurableApplicationContext {

    @Override
    public void refresh() {
        //准备beanFactory，并加载BeanDefinition
        refreshBeanFactory();

        ConfigurableListableBeanFactory beanFactory = getBeanFactory();

        //在bean实例化之前执行BeanFactoryPostProcessor，允许修改BeanDefinition
        invokeBeanFactoryPostProcessor(beanFactory);

        //提前注册BeanPostProcessor
        registerBeanPostProcessor(beanFactory);

        //实例化单例bean
        beanFactory.preInstantiateSingletons();
    }

    /**
     * 注册BeanPostProcessor
     */
    private void registerBeanPostProcessor(ConfigurableListableBeanFactory beanFactory) {
        Map<String, BeanPostProcessor> beanPostProcessorMap = beanFactory.getBeansOfType(BeanPostProcessor.class);
        for (BeanPostProcessor beanPostProcessor : beanPostProcessorMap.values()) {
            beanFactory.addBeanPostProcessor(beanPostProcessor);
        }
    }

    /**
     * 执行BeanFactoryPostProcessor的postProcessorBeanFactory方法
     */
    private void invokeBeanFactoryPostProcessor(ConfigurableListableBeanFactory beanFactory) {
        Map<String, BeanFactoryPostProcessor> beanFactoryPostProcessorMap = beanFactory.getBeansOfType(BeanFactoryPostProcessor.class);
        if (!beanFactoryPostProcessorMap.isEmpty()) {
            for (BeanFactoryPostProcessor beanFactoryPostProcessor : beanFactoryPostProcessorMap.values()) {
                beanFactoryPostProcessor.postProcessorBeanFactory(beanFactory);
            }
        }
    }

    /**
     * 刷新BeanFactory
     */
    protected abstract void refreshBeanFactory();

    /**
     * 获取BeanFactory
     */
    protected abstract ConfigurableListableBeanFactory getBeanFactory();

    @Override
    public Object getBean(String beanName) {
        return getBeanFactory().getBean(beanName);
    }

    @Override
    public <T> T getBean(String beanName, Class<T> clazz) {
        return getBeanFactory().getBean(beanName, clazz);
    }

    @Override
    public Object getBean(String beanName, Object... args) {
        return getBeanFactory().getBean(beanName, args);
    }

    @Override
    public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
        return getBeanFactory().getBeansOfType(type);
    }

    @Override
    public String[] getBeanDefinitionNames() {
        return getBeanFactory().getBeanDefinitionNames();
    }
}
